const net = require('net')

function resData(str) {
    return `HTTP/1.1 200 OK
    Connection: keep-alive
    Content-Type: text/html; charset=utf-8
    Content-Length: ${str.length}
    Date:${new Date()}
    ${str}`
}

const server = net.createServer((socket) => {

    socket.on('data', (data) => {
        if (/^GET\/HTTP/.test(data)) {
            socket.write(resData('<h1>hello world</h1>'))
        }
        console.log(data.toString())
    })
    socket.on('close', () => {
        console.log('close')
    })
})

server.listen(8080, () => {
    console.log('http://127.0.0.1:8080')
})
